﻿using System.Net.Mime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Tankmovement : MonoBehaviour
{
    public float moveSpeed = 5.0f;             //坦克移动速度
    public float angSpeed = 10f;             //坦克转向速度
    public int tankID = 0;                    //坦克编号
    public AudioClip idleAudio;              //坦克停留音效
    public AudioClip drivingAudio;           //坦克行走音效
    public Image image;                      //游戏菜单画布
    public Text operText;                    //操作说明
    private new Rigidbody rigidbody;         
    private new AudioSource audio;              //音源
    
    // Start is called before the first frame update
    void Start()
    {
        audio = this.GetComponent<AudioSource>();
        rigidbody = this.GetComponent<Rigidbody>();
        image = GameObject.Find("Image").GetComponent<Image>();
        image.enabled = false;
        operText = GameObject.Find("OperText").GetComponent<Text>();
        operText.enabled = false;
    }

    void FixedUpdate()
    {
        float h = Input.GetAxisRaw("HorizontalPlayer" + tankID);
        float v = Input.GetAxisRaw("VerticalPlayer" + tankID);
        rigidbody.velocity = transform.forward * v * moveSpeed;          //移动
        rigidbody.angularVelocity = transform.up * h * angSpeed;         //转向
        Debug.Log(rigidbody.angularVelocity);

        if(h != 0 || v != 0){
            audio.clip = drivingAudio;
            if(audio.isPlaying == false)
                audio.Play();
        }else{
            audio.clip = idleAudio;
            if (audio.isPlaying == false)
                audio.Play();
        }
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey(KeyCode.Tab) || Input.GetKey(KeyCode.Keypad0)){
            image.enabled = true;
            operText.enabled = true;
        }else{
            image.enabled = false;
            operText.enabled = false;
        }
    }
}
